<?php
/**
* @filename source_class.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/
header("Content-Type: text/html;charset=utf-8");
if(!defined('IN_SITE')) {
	exit('Access Denied');
}
class course_note{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
	/*
	*	获取一条信息
	*/
	public function one_info($table_name,$key_name,$key_id='') {	
		$info = array();
		if($table_name && $key_name && $key_id){
			$sql = "SELECT * FROM ".DB::table($table_name)." WHERE ".$key_name."='".$key_id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}
    
	/*
	*  添加信息
	*/
	public function add($table_name,$insert_data=array()) {
        $insert_id = '';
        $info = $data_result = array();
        if($table_name && $insert_data){
            $insert_id = DB::insert($table_name,$insert_data,true);
        }
        if($insert_id){
        	$sql = "SELECT cm.cm_name,cn.cn_id,cn.cn_content,cn.modify_dateline,cn.cn_dateline,cn.user_id,cn.create_dateline,ch.ch_name FROM ".DB::table('course_note')."  cn
					LEFT JOIN " . DB::table('course_main') . "  cm ON cn.cm_id = cm.cm_id
					LEFT JOIN " . DB::table('course_hour') . "  ch ON ch.ch_id = cn.ch_id
					WHERE cn.cn_id =  ".$insert_id;
        	$info = DB::fetch_all($sql);
        	if($info){
	        	foreach($info AS $key => $value){
	        		$tmp = array();
	        		$tmp['id']=$value['cn_id'];
	        		$tmp['content']=$value['cn_content'];
	        		$tmp['course']=$value['cm_name'];
	        		$tmp['chapter']=$value['ch_name'];
	        		$tmp['cn_dateline']=$value['cn_dateline'];
	        		$tmp['create_dateline']=date("Y-m-d H:i:s",$value['create_dateline']);
	        		$tmp['modify_dateline']=date("Y-m-d H:i:s",$value['modify_dateline']);
	        		$tmp['user_id']=$value['user_id'];
	        		$tmp['month']=date("m",$value['modify_dateline']);
	        		$tmp['date']=date("d",$value['modify_dateline']);
	        		$tmp['hour']=date("h:i",$value['modify_dateline']);
	        		$data_result[]=$tmp;
	        	}
        	}
        }
        return $data_result;
	}
    
	/*
	*  编辑信息
	*/
	public function edit($table_name,$update_data,$where) {
        //DEBUG 定义返回 消息格式与消息代码编号
        $effect_row='';
        if($update_data && $where){
            $effect_row = DB::update($table_name,$update_data,$where);
        }
        return $effect_row;
	}
    
	/*
	*  删除信息
	*/
	public function delete($table_name,$where) {
        $return='';
        if($table_name && $where){
            //$return = DB::delete($table_name, $where, $limit=1);
            //TODO 逻辑删需要创建isdelete字段 0=未删除 1=已删除       	
            $return=DB::update($table_name,array("isdelete"=>1),$where, $limit=1);
        }
        return $return;
    }
	/*
	*  列表信息
    *
    * @param $table_name string.
    * @param $page_condition array.
    *
    * @return array
	*/
	public function index($table_name,$page_condition){
        $page_result = $data_result = array();
        $page = $page_condition['page'];
        $limit = $page_condition['limit'];
        $perpage = $page_condition['perpage'];
        $start=(($page-1) * $perpage);
        $wheresql = $page_condition['wheresql'];
        $orderby = $page_condition['orderby'];
       /*  $sql_info = "SELECT * FROM ".DB::table($table_name)." WHERE 1=1 ".$wheresql." ".$page_condition['orderby']." ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info); */
        $sql = "SELECT cm.cm_id,cm.cm_name,cn.cn_id,cn.cn_content,cn.modify_dateline,cn.user_id,cn.user_id,cn.cn_dateline,cn.create_dateline,ch.ch_id,ch.ch_name FROM ".DB::table('course_note')."  cn
				LEFT JOIN " . DB::table('course_main') . "  cm ON cn.cm_id = cm.cm_id
				LEFT JOIN " . DB::table('course_hour') . "  ch ON ch.ch_id = cn.ch_id
				WHERE 1=1 ".$wheresql." ".$page_condition['orderby']." ".DB::limit($start, $limit);
        $info = DB::fetch_all($sql);
        	
        foreach($info AS $key => $value){
        	$tmp = array();
        	$tmp['id']=$value['cn_id'];
        	$tmp['cm_id']=$value['cm_id'];
        	$tmp['ch_id']=$value['ch_id'];
        	$tmp['content']=$value['cn_content'];
        	$tmp['course']=$value['cm_name'];
        	$tmp['chapter']=$value['ch_name'];
        	$tmp['cn_dateline']=$value['cn_dateline'];
        	$tmp['create_dateline']=date("Y-m-d H:i:s",$value['create_dateline']);
        	$tmp['modify_dateline']=date("Y-m-d H:i:s",$value['modify_dateline']);
        	$tmp['user_id']=$value['user_id'];
        	$tmp['month']=date("m",$value['modify_dateline']);
        	$tmp['date']=date("d",$value['modify_dateline']);
        	$tmp['hour']=date("h:i",$value['modify_dateline']);
        	$data_result[]=$tmp;
        }       
        $sql_total_rows = "SELECT count(*) FROM ".DB::table($table_name)." cn WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        //DEBUG 列表数据返回结构
        $page_result = array(
        	'page_now'     => $page,
        	'total_page'=>max(1, ceil($sql_total_rows_result/$perpage)),
            //int 返回结果总数
            'total_rows' => $sql_total_rows_result,
            //array 一页数据数组
            'page_data' => $data_result 
        );
        return $page_result;
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $table_name string.
     * @param $page_condition array.
     *
     * @return array
     */
	public function api($table_name,$page_condition){
        $page_result = array();
        $page = $page_condition['page'];
        $limit = $page_condition['limit'];
        $perpage = $page_condition['perpage'];
        $start=(($page-1) * $perpage);
        $wheresql = $page_condition['wheresql'];
        $orderby = $page_condition['orderby'];
        $sql_info = "SELECT * FROM ".DB::table($table_name)." WHERE 1=1 ".$wheresql." ".$page_condition['orderby']." ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        $sql_total_rows = "SELECT count(*) FROM ".DB::table($table_name)." WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        //DEBUG 列表数据返回结构
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $sql_total_rows_result,
            //array 一页数据数组
            'page_data' => $sql_info_result 
        );
        return $page_result;
	}

    /*
	*  获取表字段值
    * @param $table_name string
    * @param $key_name string
    * @param $key_id string/array
    * @param $field_name array
    *
    * @return $field_value array
	*/    
    protected static function get_field_value_by_id($table_name,$key_name,$key_id,$field_name) {
		if(is_array($key_id) && !empty($key_id)){
			$key_ids = implode('_',$key_id);
			if(!isset(self::$field_value[$key_ids])) {
				$return = DB::fetch_all('SELECT '.$key_name.','.$field_name.' FROM '.DB::table($table_name)." WHERE ".$key_name." IN (".dimplode($key_id).")");
				foreach($return AS $key => $value){
					$return_tmp[$value[$key_name]] = $value[$field_name];
					unset($return[$key]);
				}
				self::$field_value[$key_ids]=$return_tmp;
				$key_id = $key_ids;
			}else{
				$key_id = $key_ids;
			}
		}else{
			if(!isset(self::$field_value[$key_id])) {
				self::$field_value[$key_id]=DB::result_first('SELECT '.$field_name.' FROM '.DB::table($table_name)." WHERE ".$key_name."='".$key_id."' LIMIT 1");
			}
		}
		return self::$field_value[$key_id];
	}
    
	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}
?>